package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

/**
 * Created by peng.gao on 2016/10/9.
 * 可设置滑动的子上下限的垂直seekBar
 */

public class BoundVerticalSeekBar extends SeekBar {
    public BoundVerticalSeekBar(Context context) {
        this(context, null);
    }

    public BoundVerticalSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BoundVerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.rotate(-90);
        canvas.translate(-getHeight(), 0);
        super.onDraw(canvas);
    }

    private OnSeekBarChangeListener mOnSeekBarChangeListener;

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
        mOnSeekBarChangeListener = l;
        super.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
    }

    private int mLowerBound = 0, mUpperBound = -1;

    public void setLowerBound(int lowerBound) {
        this.mLowerBound = lowerBound;
    }

    public void setUpperBound(int upperBound) {
        this.mUpperBound = upperBound;
    }

    public int getLowerBound() {
        return mLowerBound;
    }

    public int getUpperBound() {
        return mUpperBound;
    }

    @Override
    public synchronized void setMax(int max) {
        super.setMax(max);
        mUpperBound = max;
    }

    @Override
    public synchronized void setProgress(int progress) {
        super.setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mOnSeekBarChangeListener.onStartTrackingTouch(this);
                break;
            case MotionEvent.ACTION_MOVE:
                int i = getMax() - (int) (getMax() * event.getY() / getHeight());
                if (i <= mUpperBound && i >= mLowerBound) {
                    int progress = getProgress();
                    if (progress != i) {
                        setProgress(i);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                mOnSeekBarChangeListener.onStopTrackingTouch(this);
                break;
            case MotionEvent.ACTION_CANCEL:
                super.onTouchEvent(event);
                break;
        }
        return true;
    }

}
